import React from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import { ConfigProvider } from 'antd';
import { AuthProvider } from './contexts/AuthContext';
import ProtectedRoute from './components/ProtectedRoute';
import MainLayout from './components/Layout/MainLayout';
import { useTranslation } from 'react-i18next';
import zhCN from 'antd/locale/zh_CN';
import enUS from 'antd/locale/en_US';

import Login from './pages/Login';
import Register from './pages/Register';
import Dashboard from './pages/Dashboard/Dashboard';
import UserList from './pages/Users/UserList';
import UserForm from './pages/Users/UserForm';
import TenantList from './pages/Tenants/TenantList';
import TenantForm from './pages/Tenants/TenantForm';
import TeamList from './pages/Teams/TeamList';
import TeamForm from './pages/Teams/TeamForm';
import KnowledgeBaseList from './pages/KnowledgeBases/KnowledgeBaseList';
import KnowledgeBaseForm from './pages/KnowledgeBases/KnowledgeBaseForm';
import DocumentList from './pages/Documents/DocumentList';
import DocumentForm from './pages/Documents/DocumentForm';
import AgentList from './pages/Agents/AgentList';
import AgentForm from './pages/Agents/AgentForm';
import PluginList from './pages/Plugins/PluginList';
import PluginForm from './pages/Plugins/PluginForm';
import TokenUsageList from './pages/TokenUsage/TokenUsageList';
import LlmModelList from './pages/LlmModels/LlmModelList';
import LlmModelForm from './pages/LlmModels/LlmModelForm';

function AppContent() {
  const { i18n } = useTranslation();

  const getLocale = () => {
    return i18n.language === 'zh' ? zhCN : enUS;
  };

  return (
    <ConfigProvider locale={getLocale()}>
      <Router>
        <Routes>
          {/* Public routes */}
          <Route path="/login" element={<Login />} />
          <Route path="/register" element={<Register />} />

          {/* Protected routes */}
          <Route
            path="/"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <Dashboard />
                </MainLayout>
              </ProtectedRoute>
            }
          />

          {/* User management - Admin only */}
          <Route
            path="/users"
            element={
              <ProtectedRoute requireAdmin>
                <MainLayout>
                  <UserList />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/users/create"
            element={
              <ProtectedRoute requireAdmin>
                <MainLayout>
                  <UserForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/users/edit/:id"
            element={
              <ProtectedRoute requireAdmin>
                <MainLayout>
                  <UserForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />

          {/* Tenant management - Admin only */}
          <Route
            path="/tenants"
            element={
              <ProtectedRoute requireAdmin>
                <MainLayout>
                  <TenantList />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/tenants/new"
            element={
              <ProtectedRoute requireAdmin>
                <MainLayout>
                  <TenantForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/tenants/:id/edit"
            element={
              <ProtectedRoute requireAdmin>
                <MainLayout>
                  <TenantForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />

          {/* Teams */}
          <Route
            path="/teams"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <TeamList />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/teams/create"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <TeamForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/teams/edit/:id"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <TeamForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />

          {/* Knowledge Bases */}
          <Route
            path="/knowledge-bases"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <KnowledgeBaseList />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/knowledge-bases/create"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <KnowledgeBaseForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/knowledge-bases/edit/:id"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <KnowledgeBaseForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />

          {/* Documents */}
          <Route
            path="/documents"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <DocumentList />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/documents/upload"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <DocumentForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/documents/edit/:id"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <DocumentForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />

          {/* Agents */}
          <Route
            path="/agents"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <AgentList />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/agents/create"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <AgentForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/agents/edit/:id"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <AgentForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />

          {/* Plugins */}
          <Route
            path="/plugins"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <PluginList />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/plugins/create"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <PluginForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/plugins/edit/:id"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <PluginForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />

          {/* Token Usage */}
          <Route
            path="/token-usage"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <TokenUsageList />
                </MainLayout>
              </ProtectedRoute>
            }
          />

          {/* LLM Models */}
          <Route
            path="/llm-models"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <LlmModelList />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/llm-models/create"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <LlmModelForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />
          <Route
            path="/llm-models/edit/:id"
            element={
              <ProtectedRoute>
                <MainLayout>
                  <LlmModelForm />
                </MainLayout>
              </ProtectedRoute>
            }
          />

          {/* Fallback */}
          <Route path="*" element={<Navigate to="/" replace />} />
        </Routes>
      </Router>
    </ConfigProvider>
  );
}

function App() {
  return (
    <AuthProvider>
      <AppContent />
    </AuthProvider>
  );
}

export default App;
